import { _decorator, Component, director, Label, Node, resources, Sprite, SpriteFrame, EventTarget, game } from 'cc';
const { ccclass, property } = _decorator;
const eventTarget = new EventTarget();
@ccclass('LifeScript')
export class LifeScript extends Component {


    // @property(ProgressBar)
    // staminaBar: ProgressBar = null; // 体力进度条

    @property(Node)
    staminaText: Node = null; // 体力文本显示

    @property(Label)
    countdownLabel: Label = null; // 倒计时文本显示

    currentStamina: number = 3; // 当前体力
    maxStamina: number = 5; // 最大体力
    restoreRate: number = 1; // 每次恢复的体力
    restoreInterval: number = 300; // 每5分钟恢复1点体力，单位秒（300秒）
    countdownTime: number = 10; // 5分钟倒计时，单位秒

    restoreTimer: Node = null; // 体力恢复定时器
    countdownTimer: Node = null; // 倒计时定时器


    private totalStar: number = 0

    onLoad() {
        game.on('decreaseXing', this.decrease, this)
        this.updateStaminaUI();
        // this.updateCountdownUI();
        this.startStaminaRecovery();

    }

    setTotalStar(totalStar: number) {
        this.totalStar = totalStar
    }

    updataStar() {

    }

    //开启定时器
    startStaminaRecovery() {
        // 以秒为单位的时间间隔
        let interval = 1;
        this.schedule(this.callback, interval);
    }

    callback() {
        if (this.currentStamina >= 5) {
            // 在第六次执行回调时取消这个计时器
            this.unschedule(this.callback);
        }
        this.updateStaminaUI();

    }


    updateStaminaUI() {
        if (this.currentStamina == 5) {
            this.countdownLabel.string = 'MAX'
            return
        }
        //更新定时器
        this.countdownTime--;
        let timeStr = this.formatTime(this.countdownTime)

        this.countdownLabel.string = timeStr
        //更新数量
        if (this.countdownTime == 0 && this.currentStamina < this.maxStamina) {
            this.currentStamina += this.restoreRate;
            this.countdownTime = 300
            if (this.currentStamina > this.maxStamina) {
                this.currentStamina = this.maxStamina;
            }
            console.log(`当前体力值：${this.currentStamina}`)

            //动态加载更新体力值
            this.loadImgUpdateLeft(this.currentStamina)
        }

    }

    loadImgUpdateLeft(leftNum: number) {

        //更新前检测是否大于5
        if (this.currentStamina < 5) {
            this.startStaminaRecovery();
        }

        resources.load(`numImg/SYMB_${leftNum}/spriteFrame`, SpriteFrame, (err, spriteFrame) => {
            if (err) {
                console.error("资源加载失败: ", err);
                return;
            }
            console.log("动态加载了");

            let staminaTextSprite = this.staminaText.getComponent(Sprite);
            if (staminaTextSprite) {
                staminaTextSprite.spriteFrame = spriteFrame;
            } else {
                console.error("staminaText 没有 Sprite 组件");
            }
        });

    }


    formatTime(seconds: number): string {
        const minutes = Math.floor(seconds / 60);  // 计算分钟数
        const remainingSeconds = seconds % 60;    // 计算剩余的秒数
        if (remainingSeconds < 10) {
            return `0${minutes}:0${remainingSeconds}`;
        } else {
            return `0${minutes}:${remainingSeconds}`;
        }

    }

    //消耗星
    decrease() {
        console.log("消耗了体力1")
        if (this.currentStamina <= 0) {
            return
        }
        this.currentStamina--;
        console.log("消耗了体力2")
        //动态加载更新体力值
        this.loadImgUpdateLeft(this.currentStamina)



    }

    onDestroy() {
        game.off('decreaseXing', this.decrease, this)
    }


}


